{extend name="admin@index_layout"/}
{block name="main"}
<div class="layui-card">
    <div class="layui-card-header">插件管理</div>
    <div class="layui-card-body">
        <table class="layui-hide" id="currentTable" lay-filter="currentTable"></table>
    </div>
</div>
<script type="text/html" id="titleTpl">
{{#  if(d.status >= 0){ }}
    {{#  if(d.config == 1 ){ }}
    <a class="layui-btn layui-btn-xs" data-open='{:url("config")}?name={{d.name}}' title="插件设置 -【{{d.title}}】">设置</a>
    {{#  } }}
    {{#  if(d.status == 0){ }}
    <a class="layui-btn layui-btn-xs layui-btn-normal btn-enable" data-name="{{d.name}}">启用</a>
    {{#  } else { }}
    <a class="layui-btn layui-btn-xs layui-btn-warm btn-disable" data-name="{{d.name}}">禁用</a>
    {{#  } }}
    <a class="layui-btn layui-btn-xs layui-btn-danger btn-uninstall" data-name="{{d.name}}" data-status="{{d.status}}"  data-title="确定卸载【{{d.title}}】，进行该操作？" confirm>卸载</a>
{{#  } else { }}
    <a class="layui-btn layui-btn-xs btn-install" data-name="{{d.name}}" data-title="确定安装【{{d.title}}】，进行该操作？">安装</a>
<!--     <a class="layui-btn layui-btn-xs btn-install" data-request='{:url("install")}?name={{d.name}}' data-table="currentTable" data-title="确定安装【{{d.title}}】，进行该操作？" confirm>安装</a> -->
{{#  } }}
</script>
<button type="button" class="layui-btn layui-btn-sm" id="local-install" value="离线安装" style="display: none"/>

<script id="conflicttpl" type="text/html">
    <div class="alert alert-danger-light">
        <strong>温馨提示</strong><br>
        此插件中发现和现有系统中部分文件发现冲突！以下文件将会被影响，请备份好相关文件后再继续操作
    </div>
    <table class="layui-table">
        <thead>
        <tr>
            <th>#</th>
            <th>文件</th>
        </tr>
        </thead>
        <tbody>
        {{# for(var i=0;i < d.list.conflictlist.length;i++){ }}
        <tr>
            <th scope="row">{{i+1}}</th>
            <td>{{d.list.conflictlist[i]}}</td>
        </tr>
        {{#  }; }}
        </tbody>
    </table>
</script>

<script id="uninstalltpl" type="text/html">
    <div class="">
        <div class="">{{ d.title }}
            <p class="text-danger">卸载将会删除所有插件文件且不可找回!!! </p>
            <p class="text-danger"><input type="checkbox" name="droptables" id="droptables" data-name="{{ d.name }}"/> 删除所有插件相关数据表且不可找回!!! </p>
            <p class="text-danger">如有重要数据请备份后再操作!!!</p>
        </div>
    </div>
</script>
{/block}
{block name="script"}
<script>
layui.use(['yznTable','table','upload','laytpl'], function() {
    var yznTable = layui.yznTable,
        $ = layui.$,
        table = layui.table,
        laytpl = layui.laytpl,
        upload = layui.upload;

    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTable',
    };

    var area = [$(window).width() > 800 ? '800px' : '95%', $(window).height() > 600 ? '600px' : '95%'];

    yznTable.render({
        init: init,
        toolbar: ['refresh',
            [{
               html:'<a class="layui-btn layui-btn-sm" onclick="importFile();"><i class="iconfont icon-upload-fill"></i>&nbsp;本地安装</a>'
            }],
        ],
        url: '{:url("index")}',
        search:false,
        cols: [
            [
                { field: 'title', width: 150, title: '名称' },
                { field: 'name', width: 150, title: '标识' },
                { field: 'description', title: '描述' },
                { field: 'author', width: 90, title: '作者' },
                { field: 'version', width: 90, title: '版本' },
                { fixed: 'right', width: 160, title: '操作', templet: '#titleTpl' }
            ]
        ],
        page: {limit:20}
    });

    var install = function (name, force) {
        yzn.request.post({
            url: '{:url("install")}',
            data: {
                name: name,
                force: force ? 1 : 0,
            },
        }, function(data,res) {
            layer.closeAll();
            layer.alert('安装成功！清除浏览器缓存和框架缓存后生效！' + (data.addon.testdata ? '<br>你还可以继续导入测试数据！' : ""), {
                btn: data.addon.testdata ? ['导入测试数据', '暂不导入'] : ['确定'],
                title:'温馨提示',
                yes: function (index) {
                    if (data.addon.testdata) {
                        yzn.request.post({
                            url: '{:url("testdata")}',
                            data: {
                                name: name,
                            }
                        }, function (data,res) {
                            layer.close(index);
                            yzn.msg.success(res.msg);
                        });
                    } else {
                        layer.close(index);
                    }
                    table.reload(init.table_render_id);
                },
                icon: 1
            });
        },function(data,res) {
            if (res && res.code === -3) {
                //插件目录发现影响全局的文件
                layer.open({
                    content: laytpl($("#conflicttpl").html()).render({list:res.data}),
                    shade: 0.8,
                    area: area,
                    title:'温馨提示',
                    btn: ['继续操作', '取消'],
                    end: function () {

                    },
                    yes: function () {
                        install(name, true);
                    }
                });
            } else {
                layer.alert(res.msg, {title: '温馨提示', icon: 0});
            }
            return false;
        })
    }

    var uninstall = function (name, force, droptables) {
        yzn.request.post({
            url: '{:url("uninstall")}',
            data: {name: name, force: force ? 1 : 0, droptables: droptables ? 1 : 0}
        }, function (data,res) {
            layer.closeAll();
            yzn.msg.success(res.msg);
            table.reload(init.table_render_id);
        }, function (data,res) {
            if (res && res.code === -3) {
                //插件目录发现影响全局的文件
                layer.open({
                    content: laytpl($("#conflicttpl").html()).render({list:res.data}),
                    shade: 0.8,
                    area: area,
                    title:'温馨提示',
                    btn: ['继续操作', '取消'],
                    end: function () {

                    },
                    yes: function () {
                        uninstall(name, true, droptables);
                    }
                });
            } else {
                layer.alert(res.msg, {title: '温馨提示', icon: 0});
            }
            return false;
        });
    };

    var operate = function (name, action, force, success) {
        yzn.request.post({
            url: '{:url("state")}',
            data: {name: name, action: action, force: force ? 1 : 0}
        }, function(data,res) {
            layer.closeAll();
            if (typeof success === 'function') {
                success(res);
            }
            table.reload(init.table_render_id);
            return false;   
        }, function(data,res) {
            if (res && res.code === -3) {
                //插件目录发现影响全局的文件
                layer.open({
                    content: laytpl($("#conflicttpl").html()).render({list:res.data}),
                    shade: 0.8,
                    area: area,
                    title:'温馨提示',
                    btn: ['继续操作', '取消'],
                    end: function () {

                    },
                    yes: function () {
                        operate(name, action, true, success);
                    }
                });
            } else {
                layer.alert(res.msg, {title: '温馨提示', icon: 0});
            }
        })
    };

    var tables = [];
    $(document).on("click", "#droptables", function () {
        if ($(this).prop("checked")) {
            yzn.request.post({
                url: '{:url("get_table_list")}',
                async: false,
                data: {name: $(this).data("name")}
            }, function (res) {
                tables = res.tables;
                return false;
            });
            var html;
            html = tables.length > 0 ? '<div class="alert alert-warning-light droptablestips" style="max-width:480px;max-height:300px;overflow-y: auto;">以下插件数据表将会被删除：<br>' + tables.join("<br>") + '<br>注意：部分插件还同时会删除演示数据和关联表等</div>'
                : '<div class="alert alert-warning-light droptablestips">注意：部分插件还同时会删除演示数据和关联表等</div>';
            $(html).insertAfter($(this).closest("p"));
        } else {
            $(".droptablestips").remove();
        }
        $(window).resize();
    });

    // 点击安装
    $(document).on("click", ".btn-install", function () {
        var name = $(this).data("name");
        var title = $(this).data("title");
        yzn.msg.confirm(title, function(index){
            install(name, false);
        });
    });

    // 点击启用
    $(document).on("click", ".btn-enable", function () {
        var name = $(this).data("name");
        operate(name, 'enable', false);
    });

    // 点击禁用
    $(document).on("click", ".btn-disable", function () {
        var name = $(this).data("name");
        operate(name, 'disable', false);
    });

    // 点击卸载
    $(document).on("click", ".btn-uninstall", function () {
        var name = $(this).data('name');
        var title = $(this).data('title');
        if ($(this).data('status') == 1) {
            layer.alert('请先禁用插件再进行卸载', {icon: 7});
            return false;
        }
        yzn.msg.confirm(laytpl($("#uninstalltpl").html()).render({name:name,title:title}), function (index, layero) {
            uninstall(name, false, $("input[name='droptables']", layero).prop("checked"));
        });
    });

    importFile=function (){
        $('#local-install').click();
    }

    //执行实例
    var uploadInst = upload.render({
        elem: '#local-install',
        url: '{:url("local")}',
        accept: 'file',
        exts: 'zip',
        done: function(res) {
            //上传完毕回调
            layer.alert(res.msg, {}, function(index) {
                if (res.code != 0) {
                    location.reload();
                }else{
                	layer.close(index);
                }
            });
        },
        error: function() {
            //请求异常回调
        }
    });
});
</script>
{/block}